﻿namespace UnityEditor.Controls.MenuSearchControl
{
    using System.Windows;
    using System.Windows.Controls;

    public partial class MenuSearchBox
    {
        /// <summary>
        /// Provides functionality to move a control around in the menu.
        /// </summary>
        protected class ControlInfo
        {
            /// <summary>
            /// The control which is moved.
            /// </summary>
            public Control Control { get; set; }

            /// <summary>
            /// The control's original parent.
            /// </summary>
            public ItemsControl Parent { get; set; }

            /// <summary>
            /// The control's original position in the menu.
            /// </summary>
            public int Index { get; set; }

            /// <summary>
            /// Moves the control behind the specified item.
            /// </summary>
            /// <param name="previous">The control behind that the control should be moved. The control's parent must be an ItemsControl.</param>
            public void Move(FrameworkElement previous)
            {
                // Cut from old parent
                var oldParent = ((ItemsControl)this.Control.Parent);
                if (oldParent != null)
                    oldParent.Items.Remove(this.Control);

                // Insert
                var parent = previous.Parent as ItemsControl;
                parent.Items.Insert(parent.Items.IndexOf(previous) + 1, this.Control);
            }

            /// <summary>
            /// Moves the control back to it's original position.
            /// </summary>
            public void MoveBack()
            {
                // Cut from old parent
                ((ItemsControl)this.Control.Parent).Items.Remove(this.Control);

                // Insert if new parent is not null
                if (this.Parent != null)
                    this.Parent.Items.Insert(this.Index, this.Control);
            }
        }
    }
}
